package cn.infinite.security.handler;

import cn.easier.club.base.dto.ResultInfo;
import cn.infinite.security.error.Errors;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("userNamePasswordAuthenticationFailureHandler")
public class UserNamePasswordAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {

        ResultInfo<String> resultInfo = new ResultInfo<>(Errors.LOGIN_BY_PWD_ERROR, null);
        response.getWriter().write(JSON.toJSONString(resultInfo));
        response.getWriter().close();
    }
}
